﻿/****************************** Module Header ******************************\
 * Module Name:  ConfigurationHelper.cs
 * Module Version:  1.0.0 (2011-12-12)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to process the configuration file.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;

namespace UCL
{
    /// <summary>
    /// 配置信息操作类
    /// </summary>
    public class ConfigurationHelper
    {
        private static Lazy<NameValueCollection> appSettings = new Lazy<NameValueCollection>(() => ConfigurationManager.AppSettings);
        public static NameValueCollection AppSettings { get { return appSettings.Value; } }

        private static Lazy<ConnectionStringSettingsCollection> connectionStringSettings = new Lazy<ConnectionStringSettingsCollection>(() => ConfigurationManager.ConnectionStrings);
        public static ConnectionStringSettingsCollection ConnectionStringSettings { get { return connectionStringSettings.Value; } }

        /// <summary>
        /// 获取数据库连接信息
        /// <para>{0}=连接字符串;{1}=提供程序名称</para>
        /// </summary>
        /// <param name="name">配置名称</param>
        /// <returns></returns>
        public static string[] GetConnectionInfo(string name = "CurrentConnection")
        {
            ConnectionStringSettings connSetting = ConfigurationManager.ConnectionStrings[name];
            if (connSetting == null) throw new Exception("ConnectionStringSetting \"{0}\" is not define.".Fmt(name));
            return new string[] { connSetting.ConnectionString, connSetting.ProviderName };
        }

        public static ConnectionStringSettings GetConnectionSetting(string name = "CurrentConnection")
        {
            ConnectionStringSettings connSetting = ConfigurationManager.ConnectionStrings[name];
            if (connSetting == null) throw new Exception("ConnectionStringSetting \"{0}\" is not define.".Fmt(name));
            return connSetting;
        }

        /// <summary>
        /// 获取所有连接的名称
        /// </summary>
        /// <returns></returns>
        public static IList<string> GetConnectionList()
        {
            IList<string> list = new List<string>();
            foreach (ConnectionStringSettings connectionStringSetting in ConfigurationManager.ConnectionStrings)
            {
                list.Add(connectionStringSetting.Name);
            }
            return list;
        }

        /// <summary>
        /// 获取AppSetting配置信息
        /// </summary>
        /// <param name="names"></param>
        /// <returns></returns>
        public static string[] GetAppSettings(params string[] names)
        {
            string[] arrOut = new string[names.Length];
            int i = 0;
            foreach (string name in names)
            {
                arrOut[i] = ConfigurationManager.AppSettings[name];
                i++;
            }
            return arrOut;
        }

        /// <summary>
        /// 获取AppSetting中指定名称的值[如果值为空则返回默认值]
        /// </summary>
        /// <param name="name"></param>
        /// <param name="defv"></param>
        /// <returns></returns>
        public static string GetAppSettingValue(string name, string defv="")
        {
            string v=ConfigurationManager.AppSettings[name];
            return string.IsNullOrEmpty(v) ? defv : v;
        }
    }
}
